/*
 * @Author: ZhanXie
 * @Date: 2020-09-01 20:34:29
 * @LastEditors: ZhanXie
 * @LastEditTime: 2020-09-01 20:38:44
 * @Description: json schema 校验
 * @FilePath: /weibo/src/validator/_validate.js
 */

const Ajv = require('ajv')
const ajv = new Ajv({
  // allErrors: true // 输出所有的错误（比较慢）
})

/**
 * json schema 校验
 * @param {Object} schema json schema 规则
 * @param {Object} data 待校验的数据
 */
function validate(schema, data = {}) {
  const valid = ajv.validate(schema, data)
  if (!valid) {
    return ajv.errors[0]
  }
}

module.exports = validate
